package com.platform.system.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.Constants;
import com.platform.common.constant.HttpStatus;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.system.domain.dto.device.AiUiConfigDto;
import com.platform.system.domain.dto.device.HumanModelManagerDto;
import com.platform.system.domain.entity.device.AiUiConfig;
import com.platform.system.domain.entity.device.HumanModelManager;
import com.platform.system.domain.vo.device.AiUiConfigVo;
import com.platform.system.mapper.device.AiUiConfigMapper;
import com.platform.system.service.ISysAiUiConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Huangkai
 */

@Slf4j
@Service
public class SysAiUiConfigServiceImpl extends ServiceImpl<AiUiConfigMapper, AiUiConfig> implements ISysAiUiConfigService {

    @Resource
    AiUiConfigMapper aiUiConfigMapper;

    @Resource
    ISysAiUiConfigService sysAiUiConfigService;

    @Override
    public TableDataInfo selectAiUiConfigList(AiUiConfigVo aiUiConfigVo) {
        LambdaQueryWrapper<AiUiConfig> aiUiConfigLambdaQueryWrapper = new QueryWrapper<AiUiConfig>().lambda();
        aiUiConfigLambdaQueryWrapper.like(StringUtils.isNotEmpty(aiUiConfigVo.getName()), AiUiConfig::getName, aiUiConfigVo.getName());
        aiUiConfigLambdaQueryWrapper.like(StringUtils.isNotEmpty(aiUiConfigVo.getScene()), AiUiConfig::getScene, aiUiConfigVo.getScene());
        aiUiConfigLambdaQueryWrapper.like(StringUtils.isNotEmpty(aiUiConfigVo.getAppId()), AiUiConfig::getAppId, aiUiConfigVo.getAppId());
        aiUiConfigLambdaQueryWrapper.orderByDesc(AiUiConfig::getCreateTime);
        Page<AiUiConfig> page = new Page<>();
        page.setCurrent(aiUiConfigVo.getPageNum());
        page.setSize(aiUiConfigVo.getPageSize());
        IPage<AiUiConfig> aiUiConfigIPage = aiUiConfigMapper.selectPage(page, aiUiConfigLambdaQueryWrapper);
        TableDataInfo rspData = new TableDataInfo();
        List<AiUiConfig> list =  aiUiConfigIPage.getRecords();
        List<AiUiConfigDto> aiUiConfigDtoList = new ArrayList<>();
        if(list != null || list.size() > 0){
            String array = JSONArray.toJSONString(list);
            aiUiConfigDtoList = JSONArray.parseArray(array, AiUiConfigDto.class);
        }
        rspData.setRows(aiUiConfigDtoList);
        rspData.setTotal(aiUiConfigIPage.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        return rspData;
    }

    @Override
    public List<AiUiConfigDto> selectAiUiConfigListByUidList(List<String> uidList) {
        LambdaQueryWrapper<AiUiConfig> aiUiConfigLambdaQueryWrapper = new QueryWrapper<AiUiConfig>().lambda();
        aiUiConfigLambdaQueryWrapper.in(!CollectionUtils.isEmpty(uidList), AiUiConfig::getUid, uidList);
        List<AiUiConfig> aiUiConfigList = aiUiConfigMapper.selectList(aiUiConfigLambdaQueryWrapper);
        if(aiUiConfigList != null){
            String array = JSONArray.toJSONString(aiUiConfigList);
            List<AiUiConfigDto> aiUiConfigDtoList = JSONArray.parseArray(array, AiUiConfigDto.class);
            return aiUiConfigDtoList;
        }
        return null;
    }

    @Override
    public AiUiConfigDto selectAiUiConfig(String uid) {
        LambdaQueryWrapper<AiUiConfig> aiUiConfigLambdaQueryWrapper = new QueryWrapper<AiUiConfig>().lambda();
        aiUiConfigLambdaQueryWrapper.eq(AiUiConfig::getUid, uid);
        AiUiConfig aiUiConfig = aiUiConfigMapper.selectOne(aiUiConfigLambdaQueryWrapper);
        if(aiUiConfig != null) {
            AiUiConfigDto aiUiConfigDto = new AiUiConfigDto();
            BeanUtils.copyProperties(aiUiConfig, aiUiConfigDto);
            return aiUiConfigDto;
        }
        return null;
    }

    @Override
    public AjaxResult updateAiUiConfigStatus(AiUiConfigVo aiUiConfigVo) {
        AiUiConfigDto aiUiConfigDto = sysAiUiConfigService.selectAiUiConfig(aiUiConfigVo.getUid());
        if(aiUiConfigDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        AiUiConfig aiUiConfig = new AiUiConfig();
        aiUiConfig.setStatus(Constants.NUM_ZERO);
        if(aiUiConfigDto.getStatus() == Constants.NUM_ZERO){
            aiUiConfig.setStatus(Constants.NUM_ONE);
        }
        LambdaQueryWrapper<AiUiConfig> statusUpdateWrapper = new QueryWrapper<AiUiConfig>().lambda();
        statusUpdateWrapper.eq(AiUiConfig::getUid, aiUiConfigVo.getUid());
        aiUiConfig.setUpdateTime(new Date());
        aiUiConfig.update(statusUpdateWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult insertAiUiConfig(AiUiConfigVo aiUiConfigVo) {
        AiUiConfig aiUiConfig = new AiUiConfig();
        aiUiConfig.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        aiUiConfig.setAppKey(aiUiConfigVo.getAppKey());
        aiUiConfig.setAppId(aiUiConfigVo.getAppId());
        aiUiConfig.setAsrParamId(aiUiConfigVo.getAsrParamId());
        aiUiConfig.setScene(aiUiConfigVo.getScene());
        aiUiConfig.setName(aiUiConfigVo.getName());
        aiUiConfig.setSecret(aiUiConfigVo.getSecret());
        aiUiConfig.setStatus(aiUiConfigVo.getStatus());
        aiUiConfig.setCreateTime(new Date());
        aiUiConfig.insert();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteAiUiConfigList(AiUiConfigVo aiUiConfigVo) {
        LambdaQueryWrapper<AiUiConfig> aiUiConfigLambdaQueryWrapper = new QueryWrapper<AiUiConfig>().lambda();
        aiUiConfigLambdaQueryWrapper.in(AiUiConfig::getUid, aiUiConfigVo.getUidList());
        aiUiConfigMapper.delete(aiUiConfigLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateAiUiConfig(AiUiConfigVo aiUiConfigVo) {
        log.info("修改的信息{}", aiUiConfigVo);
        AiUiConfigDto aiUiConfigDto = sysAiUiConfigService.selectAiUiConfig(aiUiConfigVo.getUid());
        if(aiUiConfigDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<AiUiConfig> aiUiConfigLambdaQueryWrapper = new QueryWrapper<AiUiConfig>().lambda();
        aiUiConfigLambdaQueryWrapper.eq(AiUiConfig::getUid, aiUiConfigVo.getUid());
        AiUiConfig aiUiConfig = new AiUiConfig();
        BeanUtils.copyProperties(aiUiConfigVo, aiUiConfig);
        aiUiConfig.setUpdateTime(new Date());
        aiUiConfig.update(aiUiConfigLambdaQueryWrapper);
        return AjaxResult.success();
    }
}
